package com.shey.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * Created by SheyChen on 2015/12/4 15:02.
 * Email 1768037936@qq.com
 */
public class PhotoUtil {

    public static final int MB = 1024 * 1024;

    /**
     * 压缩位图，不能大于1M
     * @param bitmap
     * @return
     */
    public static Bitmap compressBitmap(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);

        int option = 100;
        while (baos.toByteArray().length > MB) {
            baos.reset();
            bitmap.compress(Bitmap.CompressFormat.JPEG, option, baos);
            option -= 10;
        }
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        return BitmapFactory.decodeStream(bais, null, null);
    }
}
